package com.iphone.theme;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class TheActionBar {

    protected final int POSITION_LEFT = 1;
    protected final int POSITION_RIGHT = 2;

    private final Activity mActivity;
    private final View mMainView;
    private final ImageView mBackIcon;
    private final TextView mLeftText;
    private final TextView mCenterText;
    private final TextView mRightText;

    public TheActionBar(Activity activity, View group) {
        mActivity = activity;
        mMainView = group;
        mBackIcon = (ImageView) group.findViewById(R.id.ab_icon);
        mLeftText = (TextView) group.findViewById(R.id.ab_left);
        mCenterText = (TextView) group.findViewById(R.id.ab_title);
        mRightText = (TextView) group.findViewById(R.id.ab_right);

        View.OnClickListener mClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int id = v.getId();
                if (id == R.id.ab_left_back_btn) {
                    onActionClick(v, POSITION_LEFT);
                } else if (id == R.id.ab_right) {
                    onActionClick(v, POSITION_RIGHT);
                }
            }
        };
        group.findViewById(R.id.ab_left_back_btn).setOnClickListener(mClickListener);
        mRightText.setOnClickListener(mClickListener);
    }

    public TheActionBar setTitle(String title) {
        mCenterText.setText(title);
        return this;
    }

    public TheActionBar setLeft(String title) {
        mLeftText.setText(title);
        return this;
    }

    public TheActionBar setBackground(int resId) {
        mMainView.setBackgroundResource(resId);
        return this;
    }

    public TheActionBar setHomeUpDrawable(int resId) {
        mBackIcon.setImageResource(resId);
        return this;
    }

    public TheActionBar setShowHomeUpDrawable(boolean show) {
        mBackIcon.setVisibility(show ? View.VISIBLE : View.GONE);
        return this;
    }

    public TheActionBar setRight(String title) {
        mRightText.setText(title);
        return this;
    }

    public TextView getRightText() {
        return mRightText;
    }

    public abstract void onActionClick(View view, int position);
}
